package system;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import models.Profil;

/**
 *
 * @author Akbar
 */
public class MailServer extends Thread {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private final String SMTP_PORT = "465";
    private static final String SMTP_AUTH_USER = "matahari0912";
    private static final String SMTP_AUTH_PWD = "pelangipelangi";
    private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private String from;
    private String to[];
    private String cc[];
    private String subject;
    private String message;

    public MailServer(String from, String[] to, String[] cc, String subject, String message) {
        this.from = from;
        this.to = to;
        this.cc = cc;
        this.subject = subject;
        this.message = message;
    }

    @Override
    public void run() {
        send();
    }

    public void send() {
        try {
            Profil profil = Profil.getCurrentProfil();

            // Setup mail server
            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

            // Get Session
            javax.mail.Session session = javax.mail.Session.getInstance(props,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
                        }
                    });

            // Create a message
            Message msg = new MimeMessage(session);

            // Set the from address
            InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);

            // Set the to address
            InternetAddress[] addressTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                addressTo[i] = new InternetAddress(to[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Set the cc address
            InternetAddress[] addressCC = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {
                addressCC[i] = new InternetAddress(cc[i]);
            }
            msg.setRecipients(Message.RecipientType.CC, addressCC);

            // Set email's header
            msg.addHeader(profil.nama_toko + "-Mailer", profil.nama_toko + " Email Service");

            // Setting the Subject and Content Type
            msg.setSubject("[" + profil.nama_toko + "] " + subject);
            String footer = "\n\nRegards,\n--\nManajemen " + profil.nama_toko;
            msg.setContent(message + footer, "text/plain");

            // Send message
            Transport.send(msg);
        } catch (AddressException ex) {
            Logger.getLogger(MailServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(MailServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void sendEmail(String from, String to[], String cc[], String subject, String message) {
        MailServer mailServer = new MailServer(from, to, cc, subject, message);

        // Send
        mailServer.send();

        // Send concurrent
        // mailServer.start();
    }

    public static void sendEmail(String to[], String cc[], String subject, String message) {
        sendEmail("matahari0912@gmail.com", to, cc, subject, message);
    }

    public static void sendEmail(String to[], String subject, String message) {
        sendEmail(to, new String[]{}, subject, message);
    }

    public static void sendEmail(String to, String subject, String message) {
        sendEmail(new String[]{to}, subject, message);
    }
}